<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="processInfoBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="processInfoBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>

                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="processInfoTable" lay-filter="processInfoTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="processInfoTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="processDevice">工序设备管理</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="processQuality">质检参数设置</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="processInfoForm">
    <form lay-filter="processInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
         <!--<div class="layui-form-item">
             <label class="layui-form-label">物料类型</label>
             <div class="layui-input-block">
                 <select name="productTypeId" id="productTypeId" lay-verify="required" required>
                     <option value="">请选择</option>
                 </select>
             </div>
         </div>-->
         <div class="layui-form-item">
             <label class="layui-form-label">节点名称</label>
             <div class="layui-input-block">
                 <input name="nodeNames" placeholder="请输入节点名称" type="text" class="layui-input" lay-verify="required" required/>
             </div>
         </div>
         <div class="layui-form-item">
             <label class="layui-form-label">节点编码</label>
             <div class="layui-input-block">
                 <input name="nodeCodes" placeholder="请输入节点编码" type="text" class="layui-input" lay-verify="required" required/>
             </div>
         </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任生产车间</label>
            <div class="layui-input-block">
                <select name="roomInfoId" id="roomInfoId" lay-verify="required" required>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
         <div class="layui-form-item">
             <label class="layui-form-label">部门</label>
             <div class="layui-input-block">
                 <select name="departmentId" id="departmentId">
                     <option value="">请选择</option>
                 </select>
             </div>
         </div>

         <div class="layui-form-item">
             <label class="layui-form-label">节点类型</label>
             <div class="layui-input-block">
                 <select name="nodeTypes" id="nodeTypes">
                     <option value="1">流水线节点（线排配）</option>
                     <option value="2">机加工节点（站排配）</option>
                     <option value="3">单一离散节点（站排配）</option>
                 </select>
             </div>
         </div>

         <div class="layui-form-item">
             <label class="layui-form-label">支持委外</label>
             <div class="layui-input-block">
                 <input name="isOutsource" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="是|否"/>
             </div>
         </div>
         <div class="layui-form-item">
            <label class="layui-form-label">用到工序</label>
            <div class="layui-input-block">
                <input name="isUseknifeProcess" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="是|否"/>
            </div>
        </div>
       <div class="layui-form-item">
           <label class="layui-form-label">需要质检</label>
           <div class="layui-input-block">
               <input name="isDetection" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="是|否"/>
           </div>
       </div>
      <div class="layui-form-item">
           <label class="layui-form-label">检验频率</label>
           <div class="layui-input-block">
               <input name="isDetectionAll" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="所有抽查|抽检"/>
           </div>
       </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序号</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序号" type="number" class="layui-input"
                       max="100" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" >
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
        </div>
    </div>
       <div class="layui-form-item text-right">
           <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
           <button class="layui-btn" lay-filter="processInfoFormSubmit" lay-submit>保存</button>
       </div>
    </form>
</script>


<script type="text/html" id="processDeviceForm">
    <div class="layui-form toolbar">
        <div class="layui-form-item">
            <div class="layui-inline">
                <button id="processDeviceBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            </div>
        </div>
    </div>
    <table class="layui-table" id="processDeviceTable" lay-filter="processInfoTable"></table>
</script>
<script type="text/html" id="deviceForm">
    <table class="layui-table" id="deviceTable" lay-filter="deviceTable"></table>
    <div class="layui-form-item text-right">
        <button class="layui-btn" lay-filter="deviceTableSubmit" lay-submit>保存</button>
    </div>
</script>
<!-- 质检 -->
<script type="text/html" id="processQualityForm">
    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button id="processQualityBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button id="processQualityBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="processQualityTable" lay-filter="processQualityTable"></table>
            </div>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="processQualityTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="processQualityAddForm">
    <form lay-filter="processQualityForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <!--<div class="layui-form-item">
            <label class="layui-form-label">物料类型</label>
            <div class="layui-input-block">
                <select name="productTypeId" id="productTypeId" lay-verify="required" required>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">工序</label>
            <div class="layui-input-block">
                <select name="processId" id="processId" lay-verify="required" required>.
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参数分类</label>
            <input name="categoryId" type="hidden"/>
            <div class="layui-input-block" id="selectCategory">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参数名</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入参数名" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">识别码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入识别码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">展示类型</label>
            <div class="layui-input-block">
                <select name="type" id="type" lay-verify="required" required>.
                    <option value="">请选择</option>
                    <option value="1">单选</option>
                    <option value="2">复选</option>
                    <option value="3">输入框</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="processQualityFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#processInfoTable',
            url: config.base + 'enterprisecab/setting/admin/processInfo/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'nodeNames', sort: true, title: '节点名称'}
                ,{field: 'nodeCodes', sort: true, title: '节点编码'}
                ,{field: 'nodeTypes', sort: true, title: '节点类型'}
                ,{field: 'isDetectionAll', sort: true, title: '检验频率', templet: function (d) {
                        return d.isDetectionAll ? "所有抽查" : "抽检";
                    }}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#processInfoTableBar', title: '操作', width: 400}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(processInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event === 'processDevice'){
                showProcessDevice(data);
            }else if(obj.event === 'processQuality'){
                showProcessQuality(data);
            }
        });

        //监听排序
        table.on('sort(processInfoTable)', function(obj){
            table.reload('processInfoTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#processInfoBtnAdd').click(function () {
            showEditModel();
        });



        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '760px',
                offset: '65px',
                title: data ? '修改' : '添加',
                content: $('#processInfoForm').html(),
                success: function () {

                    admin.reqSync('enterprise/system/admin/roomInfo/list?',{workShop:3},function (res) {
                        if (res.code == 200) {
                            $.each(res.data,function (index,item) {
                                $("#roomInfoId").append(new Option(item.roomName,item.id));
                            })
                            form.render('select');
                        }else{
                            layer.msg(res.msg, {icon: 2});
                        }

                    },'GET')

                    // 获取部门数据
                    admin.reqSync('user/admin/department/list', {limit:9999}, function (res) {
                        if ( res.code==200) {
                            var html='<option value="">请选择部门</option>';
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'">'+res.data[k].name+'</option>';
                            }
                            $("select[name='departmentId']").html(html);
                            form.render('select');
                        } else {
                            layer.msg('获取部门失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    form.val('processInfoForm', data);

                    // 表单提交事件
                    form.render("select")
                    if(data){
                        console.log("aaaaa")

                        var select = 'dd[lay-value=' + data.departmentId + ']';
                        $("select[name='departmentId']").siblings("div.layui-form-select").find('dl').find(select).click();
                        /*var select = 'dd[lay-value=' + data.productTypeId + ']';
                        $("select[name='productTypeId']").siblings("div.layui-form-select").find('dl').find(select).click();*/

                        $("#nodeTypes").val(data.nodeTypes);
                        form.render("select")
                    }
                    form.on('submit(processInfoFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprisecab/setting/admin/processInfo/update' : 'enterprisecab/setting/admin/processInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('processInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }
        var processId = -1;
        //工序设备管理
        function showProcessDevice(data) {

            admin.open({
                type: 1,
                area: '760px',
                offset: '65px',
                title: '工序设备管理',
                content: $('#processDeviceForm').html(),
                success: function () {
                    processId =data.id;
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#processDeviceTable',
                        url: config.base + 'enterprise/product/admin/processDevice/list?processId='+data.id,
                        page: true,
                        cellMinWidth: 100,
                        // toolbar: '#toolbar',
                        defaultToolbar: false,
                        cols: [[
                            {type: 'checkbox'}
                            // , {field: 'id', sort: true, title: 'ID'}
                            , {type: 'numbers',  title: '序号'}
                            , {field: 'deviceModelName', sort: false, title: '设备型号'}
                            , {field: 'deviceCode', sort: true, title: '设备编码'}
                            , {field: 'deviceTypeName', sort: false, title: '设备类型'}
                            , {field: 'deviceBrandName', sort: false, title: '设备品牌'}
                            , {field: 'categoryName', sort: false, title: '所属分类'}
                            , {field: 'supplierName', sort: false, title: '供应商'}
                            , {field: 'description', sort: false, title: '备注描述'}
                            , {field: 'statusDesc', sort: false, title: '状态'}
                            , {field: 'creatorName', sort: false, title: '录入人'}
                            , {field: 'createTime', sort: true, title: '添加时间'}

                        ]],
                        done: function (res, curr, count) {

                        }
                    });
                    // 添加按钮点击事件
                    $('#processDeviceBtnAdd').click(function () {
                        showDevice();
                    });

                    //生产设备列表
                    function showDevice(){
                        admin.open({
                            type: 1,
                            area: '760px',
                            offset: '65px',
                            title: '生产设备列表',
                            content: $('#deviceForm').html(),
                            success: function () {

                                var insTb = table.render({
                                    elem: '#deviceTable',
                                    url: config.base + 'enterprise/product/admin/processDevice/processList?processId='+processId,
                                    page: true,
                                    cellMinWidth: 100,
                                    // toolbar: '#toolbar',
                                    defaultToolbar: false,
                                    cols: [[
                                        {type: 'checkbox'}
                                        // , {field: 'id', sort: true, title: 'ID'}
                                        , {type: 'numbers',  title: '序号'}
                                        , {field: 'deviceModelName', sort: false, title: '设备型号'}
                                        , {field: 'deviceCode', sort: true, title: '设备编码'}
                                        , {field: 'deviceTypeName', sort: false, title: '设备类型'}
                                        , {field: 'deviceBrandName', sort: false, title: '设备品牌'}
                                        , {field: 'categoryName', sort: false, title: '所属分类'}
                                        , {field: 'supplierName', sort: false, title: '供应商'}
                                        , {field: 'description', sort: false, title: '备注描述'}
                                        , {field: 'statusDesc', sort: false, title: '状态'}
                                        , {field: 'creatorName', sort: false, title: '录入人'}
                                        , {field: 'createTime', sort: true, title: '添加时间'}

                                    ]],
                                    done: function (res, curr, count) {

                                    }


                                });
                                form.on('submit(deviceTableSubmit)', function () {
                                    let checkStatus = table.checkStatus('deviceTable').data;
                                    var codes = [];
                                    for (var i = 0; i < checkStatus.length; i++) {
                                        codes.push(checkStatus[i].deviceCode)
                                    }
                                    admin.req('enterprise/product/admin/processDevice/add',JSON.stringify({processId:processId,deviceCode: codes}), function (res) {
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    },'POST', true, 'application/json; charset=utf-8');

                                });
                            }
                        })
                    }
                }
            })

        }

        function showProcessQuality(data) {
            admin.open({
                type: 1,
                area: '760px',
                offset: '65px',
                title: '质检参数设置',
                content: $('#processQualityForm').html(),
                success: function () {
                    var insTb = table.render({
                        elem: '#processQualityTable',
                        url: config.base + 'production/qualityTesting/admin/processQuality/list',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{field: 'productTypeName', title: '物料类型'}
                            ,{field: 'processName', title: '工序'}
                            ,{field: 'categoryName',  title: '分类'}
                            ,{field: 'name',  title: '参数名'}
                            ,{field: 'code',  title: '识别码'}
                            ,{field: 'type', title: '展示类型'}
                            ,{field: 'createTime', title: '创建时间'}
                            ,{align: 'center', toolbar: '#processQualityTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {
                            // 添加按钮点击事件
                            $('#processQualityBtnAdd').click(function () {
                                showProcessQuality();
                            });
                            function showProcessQuality(data) {
                                admin.open({
                                    type: 1,
                                    area: '760px',
                                    offset: '65px',
                                    title: '质检参数设置',
                                    content: $('#processQualityAddForm').html(),
                                    success: function () {
                                        var treeData=[];
                                        admin.req('enterprise/product/admin/processQualityCategory/getTreeData', {limit:9999}, function (res) {
                                            if ( res.code==200) {
                                                treeData=res.data;
                                            } else {
                                                layer.msg('获取质量参数分类失败', {icon: 2, time: 1000});
                                            }
                                        }, 'GET',false);
                                       /* admin.reqSync('enterprise/product/admin/productType/getAllKnife',{},function (res) {
                                            if (res.code == 200) {
                                                $.each(res.data,function (index,item) {
                                                    $("#productTypeId").append(new Option(item.name,item.id));
                                                })
                                                form.render('select');
                                            }else{
                                                layer.msg(res.msg, {icon: 2});
                                            }

                                        },'GET')*/

                                        admin.reqSync('enterprisecab/setting/admin/processInfo/list',{limit:9999},function (res) {
                                            if (res.code == 200) {
                                                console.log(res.data)
                                                $.each(res.data,function (index,item) {
                                                    $("#processId").append(new Option(item.nodeNames,item.id));
                                                })
                                                form.render('select');
                                            }else{
                                                layer.msg(res.msg, {icon: 2});
                                            }

                                        },'GET')



                                        xmSelect.render({
                                            el: '#selectCategory',
                                            model: {label: {type: 'text'}},
                                            radio: true,
                                            clickClose: true,
                                            filterable: true,
                                            tree: {
                                                show: true,
                                                strict: false,
                                                expandedKeys: [-1],
                                            },
                                            tips: '顶级分类',
                                            height: 'auto',
                                            data() {
                                                return treeData;
                                            },
                                            on: function (data) {
                                                //被选中的select的id,如果没有 -1
                                                let categoryId = data.arr.length > 0 ? data.change[0].value : -1;
                                                $("input[name='categoryId']").val(categoryId);
                                            },
                                        });



                                        form.val('processQualityForm', data);
                                        // 表单提交事件
                                        form.on('submit(processQualityFormSubmit)', function (d) {
                                            layer.load(2);
                                            admin.req(data ? 'production/qualityTesting/admin/processQuality/update' : 'production/qualityTesting/admin/processQuality/add', d.field, function (res) {
                                                layer.closeAll('loading');
                                                if (res.code == 200) {
                                                    layer.msg(res.msg, {icon: 1});
                                                    table.reload('processQualityTable');
                                                    layer.closeAll('page');
                                                } else {
                                                    layer.msg(res.msg, {icon: 2});
                                                }
                                            }, data ? 'PUT' : 'POST');
                                            return false;
                                        });
                                    }
                                })
                            }
                        }
                    });
                }

            })
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/setting/admin/processInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#processInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('processInfoTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>